﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using BookStore.Bussiness;
using BookStore.Data;

namespace BookStore
{
    public partial class Search : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
           
            if (!IsPostBack)
            {
                LoadComboBox();
                if (Request.QueryString["Keywords"] == null)
                {
                    return;
                }
                else
                {
                    txtsearch.Text = Request.QueryString["Keywords"].ToString();
                    //GridView1.DataSource = xuly.Thucthidulieu("SELECT * FROM tblSACH Where Tensach like'%" + Request.QueryString["Text"] + "%'", CommandType.Text);
                    Repeater1.DataSource = productController.Get_productname(Request.QueryString["Keywords"]);
                    Repeater1.DataBind();
                }
            }

        }
        private void LoadComboBox()
        {
            ddlCategory.Items.Clear();
            ddlCategory.Items.Add(new ListItem("Select Catagory", "-1"));
            DataTable tbl = catogoryController.Get_catalogALL();

            foreach (DataRow r in tbl.Rows)
            {
                ddlCategory.Items.Add(new ListItem(r["catogryName"].ToString(), r["catogoryID"].ToString()));
            }

            //ddlCategory.DataSource = catogoryController.Get_catalogALL();
            //ddlCategory.DataTextField = "catogoryID";
            //ddlCategory.DataBind();
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            Repeater1.DataSource = productController.Get_productnamewithcata(ddlCategory.SelectedItem.Text, txtsearch.Text);
            Repeater1.DataBind();
        }
        protected void lnbname_Click(object sender, EventArgs e)
        {
            LinkButton lnb = (LinkButton)sender;
            string id = lnb.CommandArgument;
            Response.Redirect("Productdetail.aspx?productId=" + id);
        }
        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            LinkButton lnb = (LinkButton)sender;
            int id = Convert.ToInt32(lnb.CommandArgument);
            productService biz = new productService();
            productInfo sp = biz.getById(id);

            Cart objCart = new Cart();
            if (Session["cart"] != null)
                objCart = (Cart)Session["cart"];
            objCart.AddToCart(sp);
            Session["cart"] = objCart;
        }
        protected void lbnimg_Click(object sender, EventArgs e)
        {
            LinkButton lnb = (LinkButton)sender;
            string id = lnb.CommandArgument;
            Response.Redirect("Productdetail.aspx?productId=" + id);
        }
    }
}
